package com.mzy.interview

/**
 * @Author: mizy3
 * @Date: 2024/08/25/下午8:40
 * @Description:
 */
class 找出字符串中第一个匹配项的下标 {
    fun strStr(haystack: String, needle: String): Int {
        var i = 0
        var j = 0
        var count = 0
        while (i < haystack.length) {
            if (haystack[i] == needle[j]) {
                j++
                count++
            }
            if (count == needle.length) {
                return (i + 1) - needle.length
            }
            i++;
        }
        return -1
    }
}

fun main() {
    println(找出字符串中第一个匹配项的下标().strStr("happybuthappy", "happy"))
}
